﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

using Duality;
using Duality.Drawing;
using Duality.Resources;

using OpenTK;
using OpenTK.Input;

using MagicPrototype.MagicSystem;

namespace MagicPrototype.TestStuff
{
	[Serializable]
    public class Hud : Component, ICmpRenderer
    {
		[NonSerialized] private CanvasBuffer buffer	= new CanvasBuffer();


		float ICmpRenderer.BoundRadius
		{
			get { return 0.0f; }
		}

		bool ICmpRenderer.IsVisible(IDrawDevice device)
		{
			return 
				(device.VisibilityMask & VisibilityFlag.ScreenOverlay) == VisibilityFlag.ScreenOverlay &&
				(device.VisibilityMask & VisibilityFlag.AllGroups) != VisibilityFlag.None;
		}
		void ICmpRenderer.Draw(IDrawDevice device)
		{
			Canvas canvas = new Canvas(device, this.buffer);
			canvas.State.SetMaterial(new BatchInfo(DrawTechnique.Alpha, ColorRgba.White));

			Player player = Scene.Current.FindComponent<Player>();
			Character character = player != null ? player.ControlObject : null;

			if (character != null)
			{
				canvas.PushState();
				canvas.State.ColorTint = ColorRgba.Red * ColorRgba.Grey;
				canvas.FillRect(
					device.TargetSize.X - 10 - 30, 
					device.TargetSize.Y - 10 - character.Health, 
					30, 
					character.Health);
				canvas.State.ColorTint = ColorRgba.White.WithAlpha(0.5f);
				canvas.DrawRect(
					device.TargetSize.X - 10 - 30, 
					device.TargetSize.Y - 10 - 100, 
					30, 
					100);
				canvas.State.ColorTint = ColorRgba.Blue * ColorRgba.Grey;
				canvas.FillRect(
					device.TargetSize.X - 10 - 30 - 10 - 30, 
					device.TargetSize.Y - 10 - character.Mana, 
					30, 
					character.Mana);
				canvas.State.ColorTint = ColorRgba.White.WithAlpha(0.5f);
				canvas.DrawRect(
					device.TargetSize.X - 10 - 30 - 10 - 30, 
					device.TargetSize.Y - 10 - 100, 
					30, 
					100);
				canvas.PopState();

				if (character.DamageReaction > 0.005f)
				{
					canvas.PushState();
					canvas.State.SetMaterial(new BatchInfo(DrawTechnique.Add, ColorRgba.White));
					canvas.State.ColorTint = ColorRgba.Red.WithAlpha(character.DamageReaction * 0.5f);
					canvas.FillRect(0, 0, canvas.Width, canvas.Height);
					canvas.PopState();
				}
			}

			SpellScriptEditor editor = Scene.Current.FindComponent<SpellScriptEditor>();
			if (editor == null || !editor.Active)
			{
				canvas.DrawText(new string[] { 
					"P:     Toggle Spell Editor",
					"WASD:  Move around",
					"Click: Cast the spell"},
					10, device.TargetSize.Y - 10, 0, Alignment.BottomLeft, true);
			}
			else
			{
				canvas.DrawText(new string[] { 
					"P:     Toggle Spell Editor",
					"F-Key: Load Spell Preset",
					"Shift+",
					"F-Key: Save Spell Preset",
					"",
					"Type on the Keyboard to create Glyphs,",
					"or just use the Mouse Wheel."},
					10, device.TargetSize.Y - 10, 0, Alignment.BottomLeft, true);
			}

			canvas.State.ColorTint = ColorRgba.White;
			canvas.FillCircle(DualityApp.Mouse.X, DualityApp.Mouse.Y, 4);
			canvas.State.ColorTint = ColorRgba.Black;
			canvas.FillCircleSegment(DualityApp.Mouse.X, DualityApp.Mouse.Y, 0, 4, 0, MathF.RadAngle360, 1.5f);
		}
	}
}
